﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializationHelper.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The serialization helper.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.Helper
{
    using System;
    using System.IO;
    using System.Runtime.Serialization;    
    using System.Text;

    /// <summary>
    /// The serialization helper.
    /// </summary>
    public class SerializationHelper
    {
        /// <summary>
        /// Serialize an object into XML
        /// </summary>
        /// <param name="serializableObject">
        /// Object that can be serialized
        /// </param>
        /// <returns>
        /// Serial XML representation
        /// </returns>
        public static string XmlSerialize(object serializableObject)
        {
            return _XmlSerialize(serializableObject);

        }

        /// <summary>
        /// The xml serialize.
        /// </summary>
        /// <param name="parameterName">
        /// paraméter neve
        /// </param>
        /// <param name="serializableObject">
        /// a serializálandó object
        /// </param>
        /// <returns>
        /// The xml serialize.
        /// </returns>
        public static string XmlSerialize(string parameterName, object serializableObject)
        {
            string xml = string.Empty;
            try
            {
                if (serializableObject != null)
                {
                    xml = string.Format("<{0}>", parameterName);
                    xml += _XmlSerialize(serializableObject);
                    xml += string.Format("/<{0}>", parameterName);
                }
            }
            catch (Exception e)
            {
                xml = e.ToString();
            }

            return xml;
        }

        /// <summary>
        /// The _ xml serialize.
        /// </summary>
        /// <param name="serializableObject">
        /// a serializálandó object
        /// </param>
        /// <returns>
        /// The _ xml serialize.
        /// </returns>
        private static string _XmlSerialize(object serializableObject)
        {
            string xml = string.Empty;
            try
            {
                if (serializableObject != null)
                {
                    var serializer = new DataContractSerializer(serializableObject.GetType());

                    var stream = new MemoryStream();
                    serializer.WriteObject(stream, serializableObject);

                    xml = Encoding.GetEncoding("UTF-8").GetString(stream.ToArray());
                    stream.Close();

                    if (Convert.ToChar(xml.Substring(0, 1)) == 65279)
                    {
                        xml = xml.Substring(1);
                    }
                }
            }
            catch (Exception ex)
            {
                // hiba esetén legalább a hibát belerakjuk
                // valami más kellene
                xml = ex.ToString();
            }

            return xml;
        }

        /// <summary>
        /// Restore (Deserialize) an object, given an XML string
        /// </summary>
        /// <param name="xmlString">
        /// XML
        /// </param>
        /// <param name="serializableObject">
        /// Object to restore as
        /// </param>
        /// <returns>
        /// The xml de serialize.
        /// </returns>
        public static object XmlDeSerialize(string xmlString, object serializableObject)
        {
            var serializer = new DataContractSerializer(serializableObject.GetType());
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
            return serializer.ReadObject(stream);
        }

        /// <summary>
        /// Restore (Deserialize) an object, given an XML string                 
        /// </summary>
        /// <param name="xmlString">
        /// XML
        /// </param>
        /// <param name="objectType">
        /// Type of object to restore as
        /// </param>
        /// <returns>
        /// The xml de serialize.
        /// </returns>
        public static object XmlDeSerialize(string xmlString, Type objectType)
        {
            var serializer = new DataContractSerializer(objectType);
            var stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
            return serializer.ReadObject(stream);
        }        
    }
}